<?php

namespace app\admin\service;

use app\admin\model\BoxArticleTag;
use app\admin\model\BoxDataOverview;
use think\Db;
use think\Exception;
use think\facade\Env;

class BoxArticleTagService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxArticleTag();
    }

    public function getList()
    {
        $param = request()->param();
        // 查询条件
        $map = [];

        if (isset($param['name']) && !empty($param['name'])) {
            $map[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $map[] = ['status', '=', $param['status']];
        }
        if (!empty($param['time_range'][0]) && !empty($param['time_range'][1])) {
            $map[] = ['create_time', '>=', $param['time_range'][0]];
            $map[] = ['create_time', '<=', $param['time_range'][1]];
        }
        $data = $this->model->where($map)->order('id', 'desc')->page(PAGE, PERPAGE)->select();
        //获取数据总数
        $count = $this->model->where($map)->count();

        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data,
            "count" => $count,
        ];
    }

    public function tagList()
    {
        // 查询条件
        $map   = [];
        $map[] = ['status', '=', 1];
        $map[] = ['mark', '=', 1];
        $list  = $this->model->where($map)->field('id,name')->select();
        return message('获取成功', true, $list);
    }

    //添加/修改
    public function edit()
    {
        $param                = request()->param();
        $param['create_time'] = date('Y-m-d H:i:s');
        if (isset($param['id']) && !empty($param['id'])) {
            $this->model->update($param, ['id' => $param['id']]);
        } else {
            $param['create_time'] = date('Y-m-d H:i:s');
            $this->model->save($param);
        }

        return message();
    }

    /**
     * @Notes:修改状态
     * @Interface status
     * @return mixed
     */
    public function status()
    {
        $param = request()->param();
        return parent::status($param); // TODO: Change the autogenerated stub

    }

}
